package com.hj.service;

import com.hj.domain.Girl;
import com.hj.enums.ResultEnum;
import com.hj.exception.GirlException;
import com.hj.repository.GirlRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by Administer on 2017/6/18.
 */
@Service
public class GirlService {
    @Autowired
    private GirlRepository girlRepository;

    @Transactional
    public void insertTwo() {
        Girl girlA = new Girl();
        girlA.setAge(10);
        girlA.setCupSize("B");

        Girl girlB = new Girl();
        girlB.setAge(20);
        girlB.setCupSize("CC");
        girlRepository.save(girlA);
        girlRepository.save(girlB);
    }

    public void getAge(Integer id) throws Exception {
        Girl girl = girlRepository.getOne(id);
        Integer age = girl.getAge();
        if (age < 10) {
            //你还在上小学吧
            throw new GirlException(ResultEnum.PRIMARY_SCHOOL);
        }
        else if (age>=10 && age <16){
            //你可能还在上初中
            throw new GirlException(ResultEnum.MIDDLE_SCHOOL);
        }


    }

    /**
     * 通过id查询一个女生
     * @param id
     * @return
     */
    public Girl findOne(Integer id){
        return girlRepository.findOne(id);
    }
}
